﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;


/// <summary>
/// Short alias for a plugin instances
/// </summary>
using PluginInstanceAlias = EpisodeWizard.Core.PluginManager<EpisodeWizard.Core.QueryPlugin>.PluginInstance;


namespace EpisodeWizard.Core
{

	public class Program
	{

		/// <summary>
		/// Program is a singleton
		/// </summary>
		private static Program Instance;


		/// <summary>
		/// Processes user-defined patterns to rename the files
		/// </summary>
		public TagEngine PatternEngine;

		/// <summary>
		/// List of all the loaded plugins (one instance of each)
		/// </summary>
		public List<PluginInstanceAlias> Plugins;

		/// <summary>
		/// List of episode files to be renamed
		/// </summary>
		public List<EpisodeFile> EpisodeFiles;

		/// <summary>
		/// Event used to notify the UI that the file list has changed
		/// </summary>
		public event EventHandler FileListChanged = null;

		/// <summary>
		/// Event used to notify the UI that the values in the file list have changed
		/// </summary>
		public event EventHandler FileListValuesChanged = null;


		/// <summary>
		/// Constructor is private
		/// </summary>
		private Program()
		{
			// Load plugins
			string AppPath = Path.GetDirectoryName(Application.ExecutablePath);
			string PluginPath = Path.Combine(AppPath, "plugins");
			Plugins = PluginManager<QueryPlugin>.GetPlugins(PluginPath);

			// Create episode list
			EpisodeFiles = new List<EpisodeFile>();

			// Create tag engine
			PatternEngine = new TagEngine();
		}

		/// <summary>
		/// Creates or returns the only intance
		/// </summary>
		public static Program GetInstance()
		{
			if (Instance == null)
			{
				Instance = new Program();
			}
			return Instance;
		}

		/// <summary>
		/// Adds all the movie files in the specified folder
		/// </summary>
		public void AddFiles(string RootFolder)
		{
			List<string> Files = Helpers.GetMovieFiles(RootFolder);
			foreach (string Filepath in Files)
			{
				EpisodeFiles.Add(new EpisodeFile(Filepath, RootFolder));
			}

			OnFileListChanged();
		}

		/// <summary>
		/// Adds all the specified files
		/// </summary>
		public void AddFiles(List<string> Files)
		{
			foreach (string Filepath in Files)
			{
				EpisodeFiles.Add(new EpisodeFile(Filepath, Path.GetDirectoryName(Filepath)));
			}

			OnFileListChanged();
		}

		/// <summary>
		/// Removes all the files from the list
		/// </summary>
		public void RemoveAllFiles()
		{
			EpisodeFiles.Clear();
			OnFileListChanged();
		}


		/// <summary>
		/// Fires the FileListChanged event
		/// </summary>
		public void OnFileListChanged()
		{
			if (FileListChanged != null)
			{
				FileListChanged(this, EventArgs.Empty);
			}
		}

		/// <summary>
		/// Fires the FileListValuesChanged event
		/// </summary>
		public void OnFileListValuesChanged()
		{
			if (FileListValuesChanged != null)
			{
				FileListValuesChanged(this, EventArgs.Empty);
			}
		}
	}

}


